package com.apes.autoparts.searcher.vin;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.searcher.DataSource;
import com.apes.autoparts.searcher.model.EpcCategoryTree;
import com.apes.autoparts.searcher.repository.AutopartsDao;
import com.apes.framework.api.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class InnerCatalogSearcher<T, R> extends DomainService implements CatalogSearcher<T, R> {



    @Autowired
    AutopartsDao autopartsDao;

    @Override
    public String[] getSource() {
        return new String[]{DataSource.INNER.name()};
    }

    @Override
    public R parse(T element) {
        /***
         * 传入参数： my_model_std_id， epc_bbmc
         * 返回值： dataSource data
         */
        JSONObject param = (JSONObject) element;
        JSONObject tempJson = new JSONObject();
        if (tempJson.size() == 0) tempJson = getStandardMenu(param);
        if (tempJson.size() == 0) tempJson = getEpcMenu(param);
        return (R) tempJson;
    }


    /***
     * 获取标准目录
     */
    public JSONObject getStandardMenu(JSONObject param) {
        JSONObject returnJson = new JSONObject();
        JSONArray plxxArray = autopartsDao.getStardardCatalog(param);
        if( plxxArray.size() == 0 ) return returnJson;

        returnJson.put("dataSource", DataSource.OWNER.name());
        returnJson.put("data", getCategory(plxxArray));
        returnJson.put("viewType","tree");
        return returnJson;
    }



    /***
     * 获取epc目录
     * my_model_std_id epc_bbmc
     */

    public JSONObject getEpcMenu(JSONObject param) {
        JSONObject returnJson = new JSONObject();

        JSONArray epcMenus = autopartsDao.getEpcMenu(param);
        if( epcMenus.size() == 0 ) return returnJson;

        // 转化数据
        List<EpcCategoryTree> treeList = EpcCategoryTree.convertTree(epcMenus, "autoparts.catalogConvert");

        returnJson.put("dataSource", DataSource.EPC.name());
        returnJson.put("data", treeList);
        returnJson.put("viewType", "tree");
        return returnJson;
    }





    /***
     * 反向获取品类
     * 为保证数据的一致性( 树形结构为 id name children )
     */
    private JSONArray getCategory(JSONArray plArray) {
        JSONArray returnArray = new JSONArray();

        JSONArray currentPlArray = autopartsDao.queryCurrentPlxx(plArray);

        int size = currentPlArray.size();
        JSONArray parentArray;
        while (size != 0) {
            parentArray = autopartsDao.queryParentUnique(currentPlArray); // 获取公共的父节点

            for (int i = 0, len1 = parentArray.size(); i < len1; i++) {
                JSONObject parentJson = new JSONObject(this.invoke("autoparts.categoryConvert", parentArray.getJSONObject(i))); // 数据转化
                for (int j = 0, len2 = currentPlArray.size(); j < len2; j++) {
                    JSONObject childJson = new JSONObject(this.invoke("autoparts.categoryConvert", currentPlArray.getJSONObject(j))); // 数据转化;

                    // 将父节点和子节点进行拼接
                    if (childJson.get("parentId") != null && childJson.getString("parentId").equals(parentJson.getString("id"))) {
                        if (parentJson.get("children") != null) {
                            parentJson.getJSONArray("children").add(childJson);
                        } else {
                            JSONArray childArray = new JSONArray();
                            childArray.add(childJson);
                            parentJson.put("children", childArray);
                        }
                    }
                }
                if (parentJson.getIntValue("level") == 1) {
                    returnArray.add(parentJson); // 直接缓存最上级的数据
                } else {
                    parentArray.getJSONObject(i).putAll(parentJson);
                }
            }
            currentPlArray = parentArray;
            size = parentArray.size();
        }
        return returnArray;
    }


}
